package com.leedsoft.config.converters;

import org.springframework.core.convert.converter.Converter;

import java.util.Date;

/**
 * Created by leeds on 2017/11/8.
 */
public class DateConverter implements Converter<String, Date>{
    private static final String DATE_REGEX = "[1-9](\\d{9}|\\d{12})";
    @Override
    public Date convert(String source) {
        if (source == null) return null;
        if (!source.matches(DATE_REGEX)) {
            throw new IllegalArgumentException("日期参数格式错误，必须是时间戳");
        }
        source = source.length() == 10 ? source.concat("000") : source;
        return new Date(Long.parseLong(source));
    }

    public static void main(String[] args) {
        System.out.println("1992920399123".matches("[1-9](\\d{9}|\\d{12})"));

    }
}
